//
//  FKAddAddressVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/1/28.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKAddAddressVC.h"
#import "FKPlaceholderTextView.h"
#import "FKCityPicker.h"
#import "FKChooseAddressVC.h"

@interface FKAddAddressVC ()

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *mobileField;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet FKPlaceholderTextView *detailAddressView;
@property (weak, nonatomic) IBOutlet UIButton *setDefaultBtn;

@property (nonatomic, strong) NSMutableDictionary *param;

@end

@implementation FKAddAddressVC

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
    }
    return _param;
}

- (instancetype)init
{
    return [UIStoryboard storyboardWithName:@"FKAddAddressVC" bundle:nil].instantiateInitialViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.detailAddressView.placeholder = @"详细地址";
    if (self.addressDict) {
        self.title = @"编辑收货地址";
        self.nameField.text = self.addressDict[@"shperson"];
        self.mobileField.text = self.addressDict[@"mobile"];
        self.addressField.text = fkFORMAT(@"%@%@%@", self.addressDict[@"province"], self.addressDict[@"city"], self.addressDict[@"county"]);
        self.detailAddressView.text = self.addressDict[@"detail_info"];
        self.setDefaultBtn.selected = [self.addressDict[@"status"] isEqualToString:@"1"];
        
        self.param[@"province"] = self.addressDict[@"province"];
        self.param[@"city"] = self.addressDict[@"city"];
        self.param[@"county"] = self.addressDict[@"county"];
        self.param[@"address_id"] = self.addressDict[@"id"];
    }
}

- (IBAction)setDefaultBtnClicked:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
}

- (IBAction)saveBtnClicked:(UIButton *)sender
{
    if ([self.nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请填写收货人"];
        return;
    }
    if ([self.mobileField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请填写联系电话"];
        return;
    }
    if (self.mobileField.text.length < 7 || self.mobileField.text.length > 11) {
        [SVProgressHUD showInfoWithStatus:@"电话号码格式不正确"];
        return;
    }
    if ([self.detailAddressView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请填写详细地址"];
        return;
    }
    self.param[@"shperson"] = self.nameField.text;
    self.param[@"mobile"] = self.mobileField.text;
    self.param[@"detail_info"] = self.detailAddressView.text;
    self.param[@"status"] = self.setDefaultBtn.isSelected ? @"1" : @"0";
    NSString *suffix = @"m=Home&c=Mine&a=add_address";
    if (self.addressDict) {
        suffix = @"m=Home&c=Mine&a=edit_address";
    }
    sender.enabled = false;
    [FKHttpTool postWithSuffix:suffix param:self.param success:^(FKBaseResult *result) {
        sender.enabled = true;
        if (result.isSuccess) {
            if (self.addressDict) {
                [SVProgressHUD showSuccessWithStatus:@"编辑成功"];
            } else {
                [SVProgressHUD showSuccessWithStatus:@"添加成功"];
                if (self.isChooseAddress) {
                    FKChooseAddressVC *vc = [[FKChooseAddressVC alloc] init];
                    vc.needRemoveBeforeVc = true;
                    fkPushNav(vc);
                    return;
                }
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshAddressList" object:nil];
            fkPopVC;
        }
    } failure:^(NSError *error) {
        sender.enabled = true; 
    }];
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return fk_adjustW(10);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 2) {
        FKCityPicker *picker = [[FKCityPicker alloc] init];
        [picker show];
        fkWeakSelf(self);
        picker.selectedCityBlock = ^(NSDictionary *dict) {
            weakself.addressField.text = fkFORMAT(@"%@%@%@", dict[@"province"], dict[@"city"], dict[@"area"]);
            weakself.param[@"province"] = dict[@"province"];
            weakself.param[@"city"] = dict[@"city"];
            weakself.param[@"county"] = dict[@"area"];
        };
    }
}

@end
